package tc.orcl.lab4;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Locale;

public class Task1
{

	public static void main(String[] args)
	{
		try
		{
			Class.forName("oracle.jdbc.driver.OracleDriver");
			System.out.println("JDBC driver is OK.");
		}
		catch (ClassNotFoundException e)
		{
			System.out.println("Error: JDBC driver is wrong!");
			System.out.println(e.getMessage());
			e.printStackTrace();
			System.exit(0);
		}
			
		Locale locale = Locale.getDefault();
		System.out.println("Default locale is " + locale);
		Locale.setDefault(Locale.ENGLISH);
			
		String url = new String("");
		url = "jdbc:oracle:thin:@localhost:1521:XE";
			
		String user = "vadik3";
		String password  = "oxe";
		
		try
		{
			Connection con = DriverManager.getConnection(url, user, password);
			System.out.println("Connection with URL " + url + " is OK.");
			
			Locale.setDefault(locale);
			
			try
			{
				Statement st = con.createStatement();
				String sql = "SELECT "
						   + "e.employee_id, "
						   + "e.last_name, "
						   + "e.hire_date, "
						   + "e.salary "
						   + "FROM "
						   + "employees e "
						   + "ORDER BY "
						   + "e.last_name";
				ResultSet rs = st.executeQuery(sql);
				System.out.println("SELECT is OK.");
				
				System.out.printf("%s\t%16s\t%s\t%s\n",
								  "Id", "Last name", "Hire date", "Salary");
				String sresult = new String("");
				
				SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
				
				while (rs.next())
				{
					sresult = String.format("%d\t%16s\t%s\t%.2f", 
											rs.getInt(1), rs.getString(2), sdf.format(rs.getDate(3)), rs.getFloat(4));
					System.out.println(sresult);
				}
				
				rs.close();
				st.close();
			}
			catch (SQLException e)
			{
				System.out.println("Error: SELECT is wrong!");
				System.out.println(e.getMessage());
			}
			
			con.close();
		}		
		catch (SQLException e)
		{
			System.out.println("Error: Connection is wrong!");
			e.printStackTrace();
		}
		
	}
	
}
